# Contributor: Leo <thinkabit.ukim@gmail.com>
# Contributor: Pedro Filipe <xpecex@outlook.com>
# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Contributor: Antoine Fontaine <antoine.fontaine@epfl.ch>
# Maintainer: Krassy Boykinov <kboykinov@teamcentrixx.com>
pkgname=pulseaudio
pkgver=17.0
pkgrel=0
pkgdesc="featureful, general-purpose sound server"
provider_priority=10
url="https://www.freedesktop.org/wiki/Software/PulseAudio/"
arch="all"
license="LGPL-2.1-or-later"
makedepends="
	meson
	tdb-dev
	alsa-lib-dev
	libasyncns-dev
	dbus-dev
	glib-dev
	gtk+3.0-dev
	orc-dev
	orc-compiler
	libsndfile-dev
	soxr-dev
	libx11-dev
	libxcb-dev
	libice-dev
	libsm-dev
	libxtst-dev
	avahi-dev
	sbc-dev
	fftw-dev
	jack-dev
	openssl-dev>3
	speexdsp-dev
	eudev-dev
	libcap-dev
	bluez-dev
	check-dev
	libtool
	perl
	perl-xml-parser
	m4
	gstreamer-dev
	gst-plugins-base-dev
	"
depends_openrc="alsa-utils alsa-utils-openrc"
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-bluez
	libpulse-mainloop-glib:_libpulse_mainloop_glib
	$pkgname-alsa
	$pkgname-utils
	$pkgname-jack
	$pkgname-zeroconf
	$pkgname-openrc
	$pkgname-bash-completion
	$pkgname-zsh-completion
	$pkgname-lang
	$pkgname-equalizer
	libpulse:_libpulse
	"
install="pulseaudio.post-install"
source="https://freedesktop.org/software/pulseaudio/releases/pulseaudio-$pkgver.tar.xz
	link-libintl.patch
	remove-once-test.patch
	$pkgname.initd
	$pkgname.confd
	"

case "$CARCH" in
	x86|x86_64|aarch64|armhf|armv7|riscv64)
		makedepends="$makedepends webrtc-audio-processing-1-dev"
		_webrtc_aec="enabled"
		;;
	* )
		_webrtc_aec="disabled" # webrtc-audio-processing not available
		;;
esac

prepare() {
	default_prepare

	sed "s|sysconfdir, 'dbus-1'|datadir, 'dbus-1'|" \
		-i src/daemon/meson.build
}

build() {
	abuild-meson \
		-Db_lto=true \
		-Dgcov=false \
		-Dman=true \
		-Dtests="$(want_check && echo true || echo false)" \
		-Dsystem_user=pulse \
		-Dsystem_group=pulse \
		-Ddatabase=tdb \
		-Dalsa=enabled \
		-Dasyncns=enabled \
		-Davahi=enabled \
		-Dbluez5=enabled \
		-Ddbus=enabled \
		-Dfftw=enabled \
		-Dglib=enabled \
		-Dgsettings=enabled \
		-Dgtk=enabled \
		-Dhal-compat=false \
		-Dipv6=true \
		-Djack=enabled \
		-Dlirc=disabled \
		-Dopenssl=enabled \
		-Dorc=enabled \
		-Dsamplerate=disabled \
		-Dsoxr=enabled \
		-Dspeex=enabled \
		-Dsystemd=disabled \
		-Dudev=enabled \
		-Dx11=enabled \
		-Dudevrulesdir=/usr/lib/udev/rules.d \
		-Dgstreamer=enabled \
		-Dwebrtc-aec="$_webrtc_aec" \
		-Ddoxygen=false \
		-Dstream-restore-clear-old-devices=true \
		. output
	meson compile -C output
}

check() {
	XDG_RUNTIME_DIR="$(mktemp -d)" \
	meson test --no-rebuild --print-errorlogs -C output
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname

	# Assumes that any volume adjustment is intended by the user, who can control
	# each app's volume. Misbehaving clients can trigger earsplitting volume
	# jumps. App volumes can diverge wildly and cause apps without their own
	# volume control to fall below sink volume; a sink-only volume control will
	# suddenly be unable to make such an app loud enough.
	sed '/flat-volumes/iflat-volumes = no' -i "$pkgdir"/etc/pulse/daemon.conf

	# Disable cork-request module, can result in e.g. media players unpausing
	# when there's a Skype call incoming
	sed 's|/usr/bin/pactl load-module module-x11-cork-request|#&|' \
		-i "$pkgdir"/usr/bin/start-pulseaudio-x11

	# Required by qpaeq
	sed '/Load several protocols/aload-module module-dbus-protocol' \
		-i "$pkgdir"/etc/pulse/default.pa
}

openrc() {
	replaces="$pkgname-system" # Backward compatibility
	default_openrc
}

_libpulse() {
	pkgdesc="Pulseaudio libraries"
	replaces="$pkgname-libs"

	amove usr/lib/pulseaudio/libpulse*
	amove usr/lib/libpulse.so.0*
	amove usr/lib/libpulse-simple.so.0*
	amove etc/pulse/client.conf
}

_libpulse_mainloop_glib() {
	pkgdesc="Pulseaudio mainloop-glib library"

	amove usr/lib/libpulse-mainloop-glib.so.*
}

bluez() {
	pkgdesc="Pulseaudio Bluetooth support"
	install_if="$pkgname=$pkgver-r$pkgrel bluez"
	provider_priority=10  # highest (other provider is pipewire-pulse)

	amove usr/lib/pulseaudio/modules/*bluez*.so
	amove usr/lib/pulseaudio/modules/*bluetooth*.so
}

alsa() {
	pkgdesc="Pulseaudio ALSA support"
	install_if="$pkgname=$pkgver-r$pkgrel alsa-lib"

	amove usr/lib/pulseaudio/modules/*alsa*.so
}

utils() {
	pkgdesc="Pulseaudio utilities"

	amove usr/bin/pa*
	amove usr/bin/start-pulseaudio-x11
}

jack() {
	pkgdesc="Pulseaudio JACK support"

	amove usr/lib/pulseaudio/modules/*jack*.so
}

zeroconf() {
	pkgdesc="Pulseaudio Zeroconf support"
	depends="avahi"

	amove usr/lib/pulseaudio/modules/*avahi*.so
	amove usr/lib/pulseaudio/modules/*zeroconf*.so
	amove usr/lib/pulseaudio/modules/*raop*.so
}

equalizer() {
	pkgdesc="Equalizer for $pkgname"
	depends="pulseaudio=$pkgver-r$pkgrel py3-qt5 py3-dbus"

	amove usr/bin/qpaeq
	amove usr/lib/pulseaudio/modules/module-equalizer-sink.so
}

sha512sums="
be0aec46204d9c9188a796fbe41b4cf6f0f5e6b16fa08ce359b8f0f51253f0ade364b89448bbf5faa2af7e59deb6c72194734c3233944250dcfd4f31968a5e97  pulseaudio-17.0.tar.xz
2c31c5bc592e748248215f8f62f85687cfec230b05f65441e6dafa5fa77d4967e97636209b2011a4501ed1337ecd880b264baa175586fc85183a980846cb8146  link-libintl.patch
c1223cf42538d91f31dbcb1a167537a1d3b6e8a3bf1b454cdebf95c25ca6f3b444848e9d546f092988851f71fe2c9dd9c4a781a58795e2f57d98563963a4630a  remove-once-test.patch
d3a95453361d4b12e2d4df73a72bbdc9343e2a46e8f5f3aac07b3cb1f9e016fd89816745f485468d144911900a2b38741a371f91cf08285c4b4988e4403d91a0  pulseaudio.initd
75b54581591519d63a3362b155c0f9b0501a60763ab394693a456c44d0216138cf3a40bdd0f7442028663bc045e9ffee286f8f8eaf2ee3bb17379b43615fee0e  pulseaudio.confd
"
